package model

import (
	"fmt"
	"choujiang/common/db"
	"choujiang/common/utils"
)

type User struct {
	Id        int64  `xorm:"INT(11) not null pk autoincr"`
	UniqueId  string `xorm:"VARCHAR(50) not null unique"`
	ShareCode string `xorm:"VARCHAR(50) not null unique"`

	Sex              int    `xorm:"INT(11) not null default 0 comment('0未知/1男/2女')"`
	NickName         string `xorm:"VARCHAR(128) not null"`
	HeadImgUrl       string `xorm:"VARCHAR(256) not null default ''"`
	HeadImgUpdatedAt int64  `xorm:"INT(11) not null default 0"`
	Province         string `xorm:"VARCHAR(50) not null default ''"`
	City             string `xorm:"VARCHAR(50) not null default ''"`

	GameId      string `xorm:"VARCHAR(50) not null default ''"`
	MiniOpenId  string `xorm:"not null VARCHAR(50) index unique"`
	MiniUnionId string `xorm:"not null default '' VARCHAR(50) index"`
	FromGame    string `xorm:"not null default '' VARCHAR(50)"`
	FromChannel string `xorm:"not null default '' VARCHAR(50)"`
	FromScene   string `xorm:"not null default '' VARCHAR(50)"`

	CreatedAt int64 `xorm:"not null default 0 INT(11) created"`
	UpdatedAt int64 `xorm:"not null default 0 INT(11) updated"`
	DeletedAt int64 `xorm:"not null default 0 INT(11)"`
	Status    int   `xorm:"not null default 0 comment('0正常/1删除/2禁用') INT(11) index"`
	DlId      int64 `xorm:"-"`

	Text1 string `xorm:"not null default '' VARCHAR(50) index"`
	Text2 string `xorm:"not null default '' VARCHAR(50) index"`
	Text3 string `xorm:"not null default '' VARCHAR(50) index"`
}

func GetUserByMiniOpenId(openId string) *User {
	user := new(User)
	has, err := db.MysqlEngine.Where("mini_open_id=?", openId).Get(user)
	if !has || err != nil {
		return nil
	}
	user.NickName = utils.DecodeNickName(user.NickName)
	return user
}

func GetUserByMiniUnionId(unionId string) *User {
	user := new(User)
	has, err := db.MysqlEngine.Where("mini_union_id=?", unionId).Get(user)
	if !has || err != nil {
		return nil
	}
	user.NickName = utils.DecodeNickName(user.NickName)
	return user
}

func GetUserByMiniProgramUnionId(unionId string) *User {
	user := new(User)
	has, err := db.MysqlEngine.Where("mini_union_id=?", unionId).In("game_id", []string{"leyoubox", "redbox"}).Where("game_id not like ?", "mp_%").Get(user)
	if !has || err != nil {
		return nil
	}
	user.NickName = utils.DecodeNickName(user.NickName)
	return user
}

func GetUserByGameIdOpenId(gameId, openId string) *User {
	fmt.Printf("-----GetUserByGameIdOpenId gameId: %s, openId: %s", gameId, openId)
	user := new(User)
	has, err := db.MysqlEngine.Where("mini_open_id=? and game_id=?", openId, gameId).Get(user)
	fmt.Printf("-----GetUserByMiniUnionId has: %d, err: %s", has, err)
	if !has || err != nil {
		fmt.Printf("-----GetUserByMiniUnionId err %+v\n", err)
		return nil
	}
	user.NickName = utils.DecodeNickName(user.NickName)
	return user
}

func GetUserByUniqueId(uniqueId string) *User {
	user := new(User)
	has, err := db.MysqlEngine.Where("unique_id=?", uniqueId).Get(user)
	if !has || err != nil {
		return nil
	}
	user.NickName = utils.DecodeNickName(user.NickName)
	return user
}

func GetUserByUid(userId int64) *User {
	user := new(User)
	has, err := db.MysqlEngine.Where("id=?", userId).Get(user)
	if !has || err != nil {
		fmt.Printf("GetUserByUid err %+v\n", err)
		return nil
	}
	user.NickName = utils.DecodeNickName(user.NickName)
	return user
}

func (u *User) Insert() (*User, error) {
	_, err := db.MysqlEngine.InsertOne(u)
	return u, err
}

func (u *User) Update() (*User, error) {
	_, err := db.MysqlEngine.ID(u.Id).Update(u)
	return u, err
}
